package com.zjt.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: desktop2
 * Date: 12-6-19
 * Time: 上午8:08
 * 和时间相关的方法集合
 */
public class DateUtil {

    public static long getAge(Date birthday) {
        if(birthday == null){
           return 0;
        }
        Date date = new Date();
        long day = (date.getTime() - birthday.getTime()) / (24 * 60 * 60 * 1000) + 1;
        String year = new java.text.DecimalFormat("#.00").format(day / 365f);
        Double d_year = Double.parseDouble(year);
        return Math.round(d_year);
    }
    //获取某个时间是周几
    public static String getDayOfWeek(Date date){
        if(null == date){
           return "";
        }
        Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTime(date);
        String weekDay_str = "";
        int weekDay = calendar.get(java.util.Calendar.DAY_OF_WEEK);
        //排序是周日，周一，周二，周三，周四，周五，周六，所以需要做一个简单的转换
        switch (weekDay){
            case 1:
               weekDay_str = "周日";
               break;
            case 2:
               weekDay_str = "周一";
               break;
            case 3:
               weekDay_str = "周二";
               break;
            case 4:
               weekDay_str = "周三";
               break;
            case 5:
               weekDay_str = "周四";
               break;
            case 6:
               weekDay_str = "周五";
               break;
            case 7:
               weekDay_str = "周六";
               break;
        }
        return weekDay_str;
    }

    /**
     * 取格式化时间
     * @param day
     * @return
     */
    public static String getDayStr(Date day) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy年MM月dd日");
        return  myFormatter.format(day);
    }

    /**
     * 取当前时间  格式自定义
     * @param day
     * @param dateFormat
     * @return
     */
    public static String getDayStr(Date day,String dateFormat) {
        try {
            SimpleDateFormat myFormatter = new SimpleDateFormat(dateFormat);
            return  myFormatter.format(day);
        } catch (Exception e) {
            //
        }
        return "";
    }
    /**
     * 取当前时间  格式自定义 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurrentDateStr() {
        Date day = new Date();
        String dateFormat = "yyyy-MM-dd HH:mm:ss";
        try {

            SimpleDateFormat myFormatter = new SimpleDateFormat(dateFormat);
            return  myFormatter.format(day);
        } catch (Exception e) {
            //
        }
        return "";
    }

    /**
     * 取当前时间  格式自定义
     * @return
     */
    public static String getCurrentDateStr(String dateFormat) {
        Date day = new Date();
        try {

            SimpleDateFormat myFormatter = new SimpleDateFormat(dateFormat);
            return  myFormatter.format(day);
        } catch (Exception e) {
            //
        }
        return "";
    }
    /**
     * 取当前时间  格式自定义 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date getCurrentDate() {
        Date day = new Date();
        String dateFormat = "yyyy-MM-dd HH:mm:ss";
        try {

            SimpleDateFormat myFormatter = new SimpleDateFormat(dateFormat);
            return  myFormatter.parse(myFormatter.format(day));
        } catch (Exception e) {
            //
        }
        return null;
    }
}
